#include "abstractpipe.h"
#include<QGraphicsScene>
#include<QPainter>
#include"globalgamemsger.h"
QImage AbstractPipe::PipeImg(":/ImgSrc/Images/pipe.png");
void AbstractPipe::afterUpdateXY()
{
    if(x<=scene()->sceneRect().width()/2&&Uncounted)
    {
        emit globalGameMsger::m.addScore();
        Uncounted=0;
    }
    if(x<-PipeImg.width()-20)
    {
        scene()->removeItem(this);
        delete this;
    }
}
AbstractPipe::AbstractPipe(QGraphicsScene&scene,bool upper,float y0,float v0,QObject*parent):QObject(parent)
{
    scene.addItem(this);
    y=y0;
    x=this->scene()->sceneRect().right();
    isupper=Uncounted=upper;
    connect(&globalGameMsger::m,&globalGameMsger::gameBegin,this,&AbstractPipe::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameOver,this,&AbstractPipe::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameRestart,this,&AbstractPipe::onGameRestart);
    vx=v0;
}

AbstractPipe::AbstractPipe(float x0, float y0,bool upper,float v0)
{
    setPos(x0,y0);
    vx=v0;
    isupper=Uncounted=upper;
    connect(&globalGameMsger::m,&globalGameMsger::gameBegin,this,&AbstractPipe::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameOver,this,&AbstractPipe::onGameOver);
    connect(&globalGameMsger::m,&globalGameMsger::gameRestart,this,&AbstractPipe::onGameRestart);
}

AbstractPipe::~AbstractPipe()
{
}

void AbstractPipe::advance(int phase)
{
    if(phase==0)return;
    updateXY();
    setPos(x,y);
    afterUpdateXY();
}

QRectF AbstractPipe::boundingRect()const
{
    return PipeImg.rect();
}

void AbstractPipe::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->drawImage(0,0,AbstractPipe::PipeImg);
}

void AbstractPipe::onGameOver()
{
    vx=0;
}
void AbstractPipe::onGameBegin()
{
}
void AbstractPipe::onGameRestart()
{
    scene()->removeItem(this);
    delete this;
}
